package com.su.song.dataapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.su.song.dataapplication.Util.ToastUtil;
import com.su.song.dataapplication.databass.UserDBHeaper;
import com.su.song.dataapplication.enity.User;


public class SQl_MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private Button btn_add;
    private Button btn_delete;
    private Button btn_search;
    private Button btn_modifi;
    private CheckBox ck_select;
    private UserDBHeaper mHelper;
    private CheckBox ck_select1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql_main);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);

        btn_add = findViewById(R.id.btn_add);
        btn_delete = findViewById(R.id.btn_delete);
        btn_search = findViewById(R.id.btn_search);
        btn_modifi = findViewById(R.id.btn_modifi);
        ck_select1 = findViewById(R.id.ck_select);

        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_search.setOnClickListener(this);
        btn_modifi.setOnClickListener(this);

    }

//    @Override
//    public void onClick(View v) {
//
//    }

    @Override
    protected void onStart() {
        super.onStart();
        //获得数据库帮助器实例
        mHelper = UserDBHeaper.getInstance(this);
        mHelper.opeaWriteLink();
        mHelper.opeaReadLink();

    }
//关闭数据库
    @Override
    protected void onStop() {
        super.onStop();
        mHelper.closeLink();
    }

    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();
        User user = null;
        switch (v.getId()){
            case R.id.btn_add:
                //添加用户信息
               user = new User(name,
                                Integer.parseInt(age),
                                Long.parseLong(height),
                                Float.parseFloat(weight),
                                ck_select1.isChecked());
               //因为返回-1是失败
                if(mHelper.insert(user) > 0){
                    ToastUtil.show(this,"添加成功");
                }
                else {
                    ToastUtil.show(this,"添加失败");
                }
                break;
            case R.id.btn_delete:
                break;
            case R.id.btn_search:
                break;
            case R.id.btn_modifi:
                break;
        }
    }
}
